package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.Topic;
import com.hpjpw.ido.service.ITopicService;
import com.hpjpw.idoapp.domain.TopicImportVo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TopicImportService {
    @Autowired
    ITopicService iTopicService;

    public boolean createTopic(TopicImportVo topicImport){
        if (StringUtils.isNull(topicImport)){
            return false;
        }
        Topic topic = new Topic();
        /* 时间格式上Topic对象已经实现了格式化 */
        topic.setContent(topicImport.getContent());
        topic.setBeginDate(topicImport.getBeginDate());
        topic.setEndDate(topicImport.getEndDate());
        topic.setTime(new Date());
        iTopicService.insertTopic(topic);
        return true;
    }

    /* 导入主题 */
    public String importTopic(List<TopicImportVo> topicImportVoList){
        if (StringUtils.isNull(topicImportVoList)||topicImportVoList.size() == 0){
            throw new CustomException("导入主题数据不能为空！");
        }
        /* 表格中成功导入与失败导入条数 */
        int successNum = 0;
        int failureNum = 0;
        /* 成功导入以及失败导入的消息提示 */
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TopicImportVo item:topicImportVoList) {
            if (createTopic(item)){
                successNum++;
                String msg = "<br/>" + successNum + "：主题内容 " + item.getContent() + " 导入成功！";
                successMsg.append(msg);
            }else {
                failureNum++;
                String msg = "<br/>" + failureNum + "：主题内容 " + item.getContent() + " 导入失败！";
                failureMsg.append(msg);
            }
        }
        return successMsg.toString();
    }
}
